package com.example.thread.basic;

/**
 *
 * 例子: 创建三个窗口卖票，总票数为100张.使用继承Thread类方式
 *
 *
 * @author readpage
 * @create 2022-08-02 23:13
 */
class Window extends Thread {

    private static int ticket = 50;
    private static Object obj = new Object();

    @Override
    public void run() {
        while (true) {
            synchronized(Window.class) {
            //synchronized(obj) {
                if (ticket > 0) {
                    try {
                        Thread.sleep(200);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    System.out.println(Thread.currentThread().getName()+ ": 卖票，票号为:" +ticket);
                    ticket--;
                } else {
                    break;
                }
            }
        }
    }
}
public class WindowTest {
    public static void main(String[] args) {
        Window t1 = new Window();
        Window t2 = new Window();
        Window t3 = new Window();

        t1.setName("窗口1");
        t2.setName("窗口2");
        t3.setName("窗口3");

        t1.start();
        t2.start();
        t3.start();
    }
}
